<!--
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/1/3-16:21
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
*-->

{extend name='public/base' /}


{block name="content"}

<div class="layui-col-md12">

    <div class="layui-card">

        <div class="layui-card-body">

            <!--表格区-->

            <div class="yys-fluid yys-wrapper">
                <div class="layui-row lay-col-space20">
                    <div class="layui-cos-xs12 layui-col-sm12 layui-col-md12 layui-col-lg12">
                        <section class="yys-body">
                            <div class="yys-body-content clearfix changepwd">
                                <div class="layui-col-lg12 layui-col-md10 layui-col-sm12 layui-col-xs12" style="width:100%">
                                    <div class="user-tables">
                                        <table id="tableFilter" lay-filter="tableFilter"> </table>
                                    </div>
                                </div>
                            </div>
                        </section>
                    </div>
                </div>
            </div>


        </div>
    </div>

</div>


{/block}


{block name="js"} <!--js处理区-->


<!--模板-->
<script type="text/html" id="operationTpl">
    {:widget('common/forms/Addbtn',array('新增子菜单',2,'add'))}
    {:widget('common/forms/Editbtn',array('编辑菜单'))}
    {:widget('common/forms/Delbtn',array('删除菜单',2))}

</script>

<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon"></i>新增菜单</button>
    <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="reload"><i class="fa fa-refresh"></i> 刷新</button>
</script>

<script type="text/html" id="status">
    <input type="checkbox" name="status"  lay-data="{{d.status}}" value="{{d.id}}"  lay-filter="sexDemo" lay-skin="switch"  lay-text="是|否"  {{ d.status == 1 ? 'checked' : '' }}>
</script>


<script>

    layui.use(['element', 'table', 'form', 'jquery', 'lucky','treetable'], function () {
        var element = layui.element;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        var lucky=layui.lucky;
        var treetable = layui.treetable;

        form.render();

        String.prototype.format = function () {
            //字符串占位符
            //eg: var str1 = "hello {0}".format("world");
            if (arguments.length == 0) return this;
            var param = arguments[0];
            var s = this;
            if (typeof (param) == 'object') {
                for (var key in param) {
                    s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
                }
                return s;
            } else {
                for (var i = 0; i < arguments.length; i++) {
                    s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
                }
                return s;
            }
        };

        table.render({
            elem: '#tableFilter',
            url:"{:url('index')}",
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            even: true, //开启隔行背景
            id:'table_id',
            height: 'full-100',
            text: {
                none: '暂无相关数据'
            },
            cols: [[
                {field: 'id',  title: 'ID',width:60},
                {field: 'html', minWidth: 100, title: '菜单名称'},
                {
                    field: 'icon', width: 70, align: 'center', templet: function (d) {
                        if (d.icon == "") {
                            return '';
                        } else {
                            var a= ('<i class="{0} {1} " ></i>').format(d.fontFamily,d.icon);
                            return a;
                        }
                    }, title: '图标'
                },
                {field:'is_menu',title:'是否显示',templet: '#status',width:90 ,unresize: true},
                {field: 'href',style:'cursor: pointer;',title: '菜单url',width:200,event: 'rehref',},
                {field: 'listorder',style:'cursor: pointer;',title: '排序',width:90,edit:'number',sort:true},
                {field: 'is_menu', title: '菜单按钮',width: 90,align: 'center',templet:function (rd) {
                        var s="";
                        if (rd.is_menu==1){
                            s='<button class="layui-btn layui-btn-xs ">菜单</button>';
                        } else {
                            s='<button class="layui-btn layui-btn-xs layui-btn-warm">按钮</button>';
                        }
                        return s;
                    }},
                {field: 'create_time', title: '添加时间',align: 'center',width:180,sort:true},
                {fixed: 'right',templet: '#operationTpl', width: 160, align: 'center', title: '操作'}
            ]],
            done: function (res) {
                layer.closeAll('loading');
            }
        });



        /**
         * 监听是否显示操作
         */
        form.on('switch(sexDemo)', function(obj){
            var _id=parseInt(obj.value);
            var pan=obj.elem.checked;
            var status;
            if (pan===false){
                status=2;
            }else {
                status=1;
            }
            lucky.Change_status("{:url('common/changeStatus')}","table_id","menu",_id,"status",status, obj.othis);
        });

        /**
         * 监听单元格编辑
         */
        table.on('edit(tableFilter)',function (obj) {
            var val=obj.value,_id=obj.data.id,field=obj.field;
            if (field=="href"){

            } else {
                var reg=/^[0-9]*$/;
                if (!reg.test(val)) {
                    layer.msg("排序必须是数字",{time:1000});
                    lucky.CreateReload("table_id");
                    return false;
                }
            }
            lucky.Change_status("{:url('common/changeStatus')}","table_id","menu",_id,field,val,obj.othis);
        });


        /**
         * 监听单行工具操作
         */
        table.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            // console.log(JSON.stringify(data));
            var _id=parseInt(data.id);
            var layEvent = obj.event;
            if (layEvent==="add"){
                var url="{:url('addMenu')}?id="+_id;
                lucky.CreateForm("添加子菜单",'55%','70%',url,"table_id");

            }else if(layEvent==="edit"){
                var urls="{:url('editMenu')}?id="+_id;
                lucky.CreateForm("编辑菜单",'55%','70%',urls,"table_id");

            }else if(layEvent==="del"){
                lucky.Delete_data(_id,"{:url('del')}","table_id","确认删除吗？有子集也会删除该菜单下的子菜单！");
            }else if(layEvent==="rehref"){

                layer.prompt({
                    formType: 3
                    ,title: '修改地址'
                    ,value: data.href
                }, function(value, index){

                    $.post("{:url('common/changeStatus')}",{id:_id,table:'menu',field:'href',status:value},function (res) {
                        if (res.code==1){
                            layer.close(index);
                            layer.msg(res.msg,{kin: 'layui-layer-lan', icon:1,time:1000},function () {
                                //同步更新表格和缓存对应的值
                                obj.update({
                                    href: value
                                });
                            });
                        }else {
                            layer.msg(res.msg,{kin: 'layui-layer-lan', icon:5,time:1000},function () {
                                layer.close(index);
                            });
                        }
                    },'json');

                });
            }
        });


        table.on('toolbar(tableFilter)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            if(obj.event==="add"){
                lucky.CreateForm("添加菜单",'55%','70%',"{:url('addMenu')}","table_id");
                return false;
            }else if(obj.event==="reload"){
                lucky.CreateReload(obj.config.id);
            }

        });


    });

</script >


{/block}
